package com.its.us.web.mapper.sys;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.its.us.web.entity.sys.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author huguangjun
 * @className RoleMapper
 * @date 2020/12/3
 * @desc 9:05
 */
public interface RoleMapper extends BaseMapper<Role> {

    /**
     * 获取角色
     *
     * @param username
     * @return
     */
    @Select("select c.* from sys_role c where c.id in(select b.role_id from sys_user_role b where b.user_id in(select a.id from sys_user a where a.username = #{username}))")
    List<Role> getCurrentRoles(String username) throws Exception;


    /**
     * 获取当前用户的角色ids
     * admin标识不可以删除（系统用户）
     *
     * @param username
     * @return
     * @throws Exception
     * @desc 没有中间表取别名id用Role实体接收
     */
    //@Select("select role_id as id from sys_user_role where user_id = (select id from sys_user where username=#{username})")
    @Select("select DISTINCT role_id as id from sys_user_role where (user_id = (select id from sys_user where username=#{username})) or role_id in(select id from sys_role where role_en='admin')")
    List<Role> getCurrentRoleIds(String username) throws Exception;

    /**
     * 根据角色id删除用户角色关联数据
     *
     * @param roleIds
     */
    @Delete("<script>" +
            "delete from sys_user_role where role_id in" +
            "<foreach collection='listIds' item='id' open='(' separator=',' close=')'>"
            + "#{id}" +
            "</foreach>" +
            "</script>")
    void deleteUserRoleByRoleIds(@Param("listIds") List<String> roleIds) throws Exception;

    /**
     * 根据角色id删除角色菜单关联数据
     *
     * @param roleIds
     */
    @Delete("<script>" +
            "delete from sys_role_menu where role_id in" +
            "<foreach collection='listIds' item='id' open='(' separator=',' close=')'>"
            + "#{id}" +
            "</foreach>" +
            "</script>")
    void deleteUserRoleMenuByRoleIds(@Param("listIds") List<String> roleIds) throws Exception;

    /**
     * get all role for tree use
     *
     * @return
     * @throws Exception
     */
    @Select("select id,role_name,role_en from sys_role")
    List<Role> getAllRole() throws Exception;

    /**
     * get role by userId
     *
     * @param userId
     * @return
     */
    @Select("select id,role_name,role_en from sys_role where id in(select role_id from sys_user_role where user_id=#{userId})")
    List<Role> getRoleByUserId(String userId);

    /**
     * 删除用户的角色
     * -add
     *
     * @param userId
     * @throws Exception
     */
    @Delete("delete from sys_user_role where user_id = #{userId}")
    void deleteUserRole(String userId) throws Exception;

    /**
     * 用户授权角色
     *
     * @param roleId
     * @param userId
     */
    @Insert("insert into sys_user_role(user_id,role_id) values(#{userId},#{roleId})")
    void insertUserRole(@Param("roleId") String roleId, @Param("userId") String userId) throws Exception;


    /**
     * 角色下的菜单权限
     * -add
     *
     * @param roleId
     * @throws Exception
     */
    @Delete("delete from sys_role_menu where role_id = #{roleId}")
    void deleteRoleMenu(String roleId) throws Exception;


    /**
     * 角色授权菜单权限
     *
     * @param menuId
     * @param roleId
     */
    @Insert("insert into sys_role_menu(menu_id,role_id) values(#{menuId},#{roleId})")
    void insertRoleMenu(@Param("menuId") String menuId, @Param("roleId") String roleId) throws Exception;
}
